package com.lzx.iteam.widget;


import android.content.Context;
import android.graphics.PointF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.FrameLayout;
import com.lzx.iteam.CloudDialerActivity;
import com.lzx.iteam.util.PreferenceUtil;

public class InterceptFrameLayout extends FrameLayout{

    private static final String TAG = "InterceptFrameLayout";

    int touchSlop;
    PointF start = new PointF();
    PointF last = new PointF();
    RotateAnimation in;
    RotateAnimation out;
    RotateAnimationListener listener;
    ChangeKeyBordListener keyBordListener;
    Context context;
    CloudDialerActivity activity;
    private String mSearchT9 = "";
    private String mSearchAllWord = "";
    boolean doubleFinger = false;
    boolean mIsT9;
    final int LEFT = 0;
    final int RIGHT = 1;
    final int UP = 2;
    final int DOWN = 3;

    int currentDir = -1;

    public InterceptFrameLayout(Context context, AttributeSet attrs) {
        super(context, attrs);

        this.context = context;
        listener = new RotateAnimationListener();
        initRotateAnimation();
        
    }
    private void initRotateAnimation(){
        in = new RotateAnimation(context);
        out = new RotateAnimation(context);
        out.setAnimationListener(listener);
    }

    public void setListener(CloudDialerActivity activity){
        this.activity = activity;
    }


    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        boolean result = false;
        touchSlop = getWidth()/2;
        int action = ev.getAction();
        switch (action & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                currentDir = -1;
                start.set(ev.getX(), ev.getY());
                last.set(ev.getRawX(),ev.getRawY());
                break;
            case MotionEvent.ACTION_POINTER_DOWN:
                if(ev.getPointerCount() == 2){
                    doubleFinger = true;
                    result = true;
                }
                if(doubleFinger)
                    return true;
                break;
            case MotionEvent.ACTION_MOVE:
                float disy = ev.getY()-start.y;
                if(Math.abs(disy)>touchSlop || doubleFinger)
                    result = true;
                break;
            default:
                break;
        }
//        return super.onInterceptTouchEvent(ev);
        return result;
    }
    int currenttop = 0;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
//        if(activity==null) return true;
        int action = event.getAction();
        switch(action & MotionEvent.ACTION_MASK){
            case MotionEvent.ACTION_DOWN:
                Log.i("finger1","222");
                currentDir = -1;
                start.set(event.getX(), event.getY());
                break;
            case MotionEvent.ACTION_POINTER_DOWN:
                Log.i("double finger","point");
                break;
            case MotionEvent.ACTION_MOVE:
                Log.i("double finger","movie");
                currenttop+=1;
                if(event.getPointerCount() == 2){
                    if(mDoubleScrollListener != null){
                        float disy = event.getRawY()-last.y;
                        mDoubleScrollListener.onDoubleScroll(disy);
                        last.set(event.getRawX(),event.getRawY());
                    }
                }
                break;
            case MotionEvent.ACTION_UP:
                float disx = event.getX()-start.x;
                float disy = event.getY()-start.y;
                if(doubleFinger){
                    doubleFinger = false;
                    if(mDoubleScrollListener != null){
                        mDoubleScrollListener.onDobleScrollUp();
                    }
                    return true;
                }
//                /*若注释掉以下代码则不能翻转切换
                
                
                if(disx!=0&&Math.abs(disx)/Math.abs(disy)<1&&Math.abs(disy)>getWidth()/2){
                    if(disy>0){
                        currentDir = DOWN;
                    }else{
                        currentDir = UP;
                    }
                }
                if(currentDir==-1){
                    return true;
                }

                View first = getChildAt(0);
                View second = getChildAt(1);
                if(currentDir==UP){
                    in.setConfig(getWidth()/2, getHeight()/2, -90, 0,false);
                    out.setConfig(getWidth()/2, getHeight()/2, 0, 90,false);
                }else{
                    out.setConfig(getWidth()/2, getHeight()/2, 0, -90,false);
                    in.setConfig(getWidth()/2, getHeight()/2, 90, 0,false);
                }
                in.reset();
                out.reset();
                in.setDuration(250);
                out.setDuration(250);
                mIsT9 = PreferenceUtil.getInstance(activity).getBoolean(PreferenceUtil.IS_T9, false);
                Log.d("search_text "," mIsT9 =============  "+  mIsT9);
                saveSearchText();
                if(second.getVisibility()==View.VISIBLE){
                    second.startAnimation(out);
                    second.setVisibility(View.GONE);
                    first.setVisibility(View.VISIBLE);
                    first.startAnimation(in);
                }else{
                    first.startAnimation(out);
                    first.setVisibility(View.GONE);
                    second.setVisibility(View.VISIBLE);
                    second.startAnimation(in);
                }
                setSearchText();
                break;
        }
        return true;
    }

    /**
     * 记录当前搜索字
     */
    public void saveSearchText(){
        if (mIsT9){
            mSearchT9 = CloudDialerActivity.instance.getFilterText();
        }else{
            mSearchAllWord = CloudDialerActivity.instance.getFilterText();
        }
    }

    /**
     * 设置搜索字
     *
     * T9 --转- 》字母键盘  设置之前字母键盘搜索字
     *
     * 字母键盘 --转- 》 T9 设置之前T9键盘搜索字
     *
     */
    public void setSearchText(){
        if (mIsT9){
            keyBordListener.allWordPad(mSearchAllWord);
        }else{
            keyBordListener.t9DialPad(mSearchT9);
        }
    }

    /**
     * 设置搜索字监听
     */
    public interface ChangeKeyBordListener{
        void t9DialPad(String searchText);
        void allWordPad(String searchText);
    }

    public void setChangeKeyBordListener(ChangeKeyBordListener listener){
        this.keyBordListener = listener;
    }

    public class RotateAnimationListener implements AnimationListener{


        public void destroyDrawingCache(ViewGroup vg){
            vg.destroyDrawingCache();
            int count = vg.getChildCount();
            for(int i=0;i<count;i++){
                View v = vg.getChildAt(i);
                if(v instanceof ViewGroup){
                    destroyDrawingCache((ViewGroup) v);
                }else{
                    v.destroyDrawingCache();
                }
            }
        }

        @Override
        public void onAnimationStart(Animation animation) {
        	

        }

        @Override
        public void onAnimationEnd(Animation animation) {
        	

            final ViewGroup first = (ViewGroup) getChildAt(0);
            final ViewGroup second = (ViewGroup) getChildAt(1);
            first.clearAnimation();
            second.clearAnimation();

            Log.e("eeeeeeeeee","public void onAnimationEnd(Animation animation)");
            
            activity.mKeyboardVertFrag.switchKeyboard(
                    !activity.mKeyboardVertFrag.mIsT9, activity.mKeyboardVertFrag.mSkin, false);

            if(second.getVisibility()==View.VISIBLE){
                activity.mKeyboardVertFrag.mIsT9 = false;
                if(!activity.mKeyboardVertFrag.mKeypadAtRight){
                    activity.mKeyboardVertFrag.mChangeDialPadAllWord_vertical_right.setVisibility(View.GONE);
                    activity.mKeyboardVertFrag.mChangeDialPadAllWord_vertical_left.setVisibility(View.VISIBLE);
                }else{
                    activity.mKeyboardVertFrag.mChangeDialPadAllWord_vertical_right.setVisibility(View.VISIBLE);
                    activity.mKeyboardVertFrag.mChangeDialPadAllWord_vertical_left.setVisibility(View.GONE);
                }
            }else{
                activity.mKeyboardVertFrag.mIsT9 = true;
                if(!activity.mKeyboardVertFrag.mKeypadAtRight){
                    activity.mKeyboardVertFrag.mChangeDialPadT9_vertical_right.setVisibility(View.GONE);
                    activity.mKeyboardVertFrag.mChangeDialPadT9_vertical_left.setVisibility(View.VISIBLE);
                }else{
                    activity.mKeyboardVertFrag.mChangeDialPadT9_vertical_right.setVisibility(View.VISIBLE);
                    activity.mKeyboardVertFrag.mChangeDialPadT9_vertical_left.setVisibility(View.GONE);
                }
            }

            PreferenceUtil.getInstance(activity).save(
                    PreferenceUtil.IS_T9, activity.mKeyboardVertFrag.mIsT9);
            InterceptFrameLayout.this.postDelayed(new Runnable() {

                @Override
                public void run() {
                    destroyDrawingCache(first);
                    destroyDrawingCache(second);
//                    activity.sendMessageToHandler(Constants.FRESH_ON_BACKGROUND);
                    System.gc();
                }
            }, 100);
			
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
        }
		
    }

    DoubleScrollListener mDoubleScrollListener;
    public interface DoubleScrollListener{
        void onDoubleScroll(float dis);
        void onDobleScrollUp();
    }

    public void setOnDoubleScrollListener(DoubleScrollListener listener){
        mDoubleScrollListener = listener;
    }


}
